﻿using Awsc.DataAcquisitionSys.Core.Common.Extensions;
using Awsc.DataAcquisitionSys.Core.DataAcquisition;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors;
using Awsc.DataAcquisitionSys.Core.Services.ConfigManagement;
using Microsoft.Extensions.DependencyInjection;

namespace Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors.Sensors
{
    internal class StringWeightSensor : Sensor
    {
        private readonly RegisterConfig config;
        private readonly DeviceTypeEnum deviceType;
        private readonly StringWeightSensorConfig abstractSensorConfig;
        public StringWeightSensor(AbstractSensorDataCollectionContext abstractSensorDataCollectionContext)
        {
            config = abstractSensorDataCollectionContext.SensorConfig as RegisterConfig;
            deviceType = abstractSensorDataCollectionContext.Service.GetService<IDeviceConfigService>().GetDeviceConfig().DeviceType;
            abstractSensorConfig = config.GetAdditionConfig<StringWeightSensorConfig>();
        }

        protected override void CalculateValue()
        {
            double d = 0;
            var oilCylinder​​Lift = GlobalDeviceManager.Instance.GetSensorValue("油缸举升力") ;
            var oilCylinderpress = GlobalDeviceManager.Instance.GetSensorValue("油缸下压力") ;
            var supportedLength = GlobalDeviceManager.Instance.GetSensorValue("举升高度") ;

            var legMode = GlobalDeviceManager.Instance.GetSensorValue("腿模式") ;
            var legModeAICreated = GlobalDeviceManager.Instance.GetSensorValue("四腿模式Coil") ;


            var sensorLift = GlobalDeviceManager.Instance.GetSensor("油缸举升力");
            var sensorPress = GlobalDeviceManager.Instance.GetSensor("油缸下压力");

            var status = DevStatus.S_OK;

            if (oilCylinder​​Lift.Value != null
                && oilCylinderpress.Value != null
                && supportedLength.Value != null)
            {

                d = oilCylinder​​Lift.ToDouble() - oilCylinderpress.ToDouble();
                if (d < 0)//管住承受的是压力
                {
                    double x = supportedLength.ToDouble() * 39.3700787;//转换为in
                    double maxp = MaxPressure(abstractSensorConfig.E, abstractSensorConfig.K, abstractSensorConfig.Fy, x, abstractSensorConfig.ID, abstractSensorConfig.OD) / 2204.6226218;//转换为吨

                    var remainMin = -maxp * 0.7 + abstractSensorConfig.PressZero;
                    var warningMin = -maxp + abstractSensorConfig.PressZero;

                    SensorDynamicSettings.Instance.AddOrUpdateSettingValue("悬重", "RemainMin", remainMin);
                    SensorDynamicSettings.Instance.AddOrUpdateSettingValue("悬重", "WarningMin", warningMin);

                }

                if (sensorLift.Status == DevStatus.S_UNKNOWN || sensorPress.Status == DevStatus.S_UNKNOWN)
                {
                    status = DevStatus.S_UNKNOWN;
                }
                else
                {
                    status = DevStatus.S_ERR;
                }
            }
            this.OutputValue = d;
            this.CurrentValue = d;
            this.Status = status;
        }
        private bool UseLegMode(SensorData legMode, SensorData legModeAI)
        {
            if (legModeAI == null && legMode == null)
            {
                return false;
            }
            if (legModeAI != null && legModeAI.ToBool())
            {
                return true;
            }
            if (legMode != null && legMode.ToBool())
            {
                return true;
            }
            return false;

        }
        public static double MaxPressure(double E, double K, double Fy, double L, double ID, double OD)
        {
            double max = 0;
            //SR = MAX(((K*L)/rg),(((Rn/t)^0.5)+(4.8*(Rn/(225*t)))))
            double I = Math.PI * (Math.Pow(OD, 4) - Math.Pow(ID, 4)) / 64;
            double A = Math.PI * (Math.Pow(OD, 2) - Math.Pow(ID, 2)) / 4;
            double Rg = Math.Sqrt(I / A);
            double Cc = Math.PI * Math.Sqrt(2 * E / Fy);
            double t = (OD - ID) / 2;
            double Rn = (ID + t) / 2;
            double J = Math.Sqrt(Rn / t) + 4.8 * (Rn / (255 * t));
            double Sr = Math.Max(K * L / Rg, J);
            //IF(Sr<Cc,(Fy*A*(1-((Sr^2)/(2*(Cc^2))))),(A*(286000000/(Sr^2))))
            if (Sr < Cc)
            {
                max = Fy * A * (1 - Sr * Sr / (2 * Cc * Cc));
            }
            else
            {
                max = A * (286000000 / (Sr * Sr));
            }
            return max;
        }
    }


}
